package com.tiandang.controller;

import com.lly835.bestpay.model.PayResponse;
import com.lly835.bestpay.model.RefundResponse;
import com.lly835.bestpay.utils.JsonUtil;
import com.tiandang.service.PayService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import tiandang.core.error.BusinessException;
import tiandang.core.model.OrderInfo;
import tiandang.core.model.OrderProduct;
import tiandang.core.response.CommonReturnType;
import tiandang.core.service.OrderService;
import tiandang.core.service.RefundNotifyService;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/wechat/order")
public class OrderController {

  private final Logger log = LoggerFactory.getLogger(this.getClass());

  @Autowired
  private OrderService orderService;

  @Autowired
  private PayService payService;

  @Autowired
  private RefundNotifyService refundNotifyService;

  //下单
  @PostMapping("/createOrder")
  public CommonReturnType createOrder(@RequestBody OrderInfo order) throws BusinessException {
    log.info("原始订单数据: {}" + JsonUtil.toJson(order));
    //创建订单
    OrderInfo orderResult = orderService.createOrder(order);
    log.info("计算后订单数据: {}" + JsonUtil.toJson(orderResult));

    //发起支付
    PayResponse payResponse = payService.create(orderResult);

    Map<String, Object> resultMap = new HashMap<>();
    resultMap.put("order", orderResult);
    resultMap.put("payResponse", payResponse);
    return CommonReturnType.success(resultMap);
  }

  //展示用户的订单列表
  @GetMapping("/list")
  public CommonReturnType list(@RequestParam(value = "openid") String openid,
                               @RequestParam(value = "currentPage") Integer currentPage,
                               @RequestParam(value = "limit", defaultValue = "10", required = false) Integer limit) {
    return CommonReturnType.success(orderService.getListByOpenid(openid, currentPage, limit));
  }


  //展示用户的订单详细
  @GetMapping("/detail")
  public CommonReturnType detail(@RequestParam(value = "orderId") String orderId) {
    OrderInfo order = orderService.getOrderById(orderId);
    List<OrderProduct> orderProductList = orderService.getOrderProductListByOrderId(orderId);
    Map<String, Object> resultMap = new HashMap<>();
    resultMap.put("order", order);
    resultMap.put("orderProductList", orderProductList);
    return CommonReturnType.success(resultMap);
  }

  //退款
  @PostMapping("/refund")
  public CommonReturnType refund(@RequestParam(value = "orderId") String orderId) {
    orderService.updateOrderStatus(orderId, 3);

    OrderInfo order = orderService.getOrderById(orderId);

    //发起退款申请
    RefundResponse refundResponse = refundNotifyService.create(order);

    Map<String, Object> resultMap = new HashMap<>();
    resultMap.put("refundResponse", refundResponse);
    return CommonReturnType.success(resultMap);
  }
}
